//*************************************************************************\\
//***********	       tdc_automata_drv_send_data_pre.cpp	***********\\
//*************************************************************************\\
//*************************************************************************\\
//*********** 		        UFPE/Cin - Greco		***********\\
//*********** 		 http://www.cin.ufpe.br/~greco 		***********\\
//***********							***********\\
//***********							***********\\
//*********** 		    By Rafael Melo Macieira		***********\\
//***********							***********\\
//***********		    E-mail: rmm2@cin.ufpe.br		***********\\
//*************************************************************************\\
//*************************************************************************\\
//**		 Automata for the temporal check of the			 **\\
//**  		drv_send_data service's pre condition 			 **\\
//*************************************************************************\\

#include <stdio.h>
#include "headers/tdc_automata_drv_send_data_pre.h"
#include "headers/address.h"

void TDC_Automata_drv_send_data_pre::printRejection(StateID stateID){
  
};

int TDC_Automata_drv_send_data_pre::checkState(StateID stateID){
   return 0;
};

int TDC_Automata_drv_send_data_pre::checkInitialState(){
  return checkState(INITIAL_STATE);
};

TDC_Automata_drv_send_data_pre::StateID TDC_Automata_drv_send_data_pre::getNextState(StateID stateID){
  switch (stateID){
  case In:
    if (access_type == 0){
      if (GET_REG_CMD(*lcd_ctrl_reg) != 44){
	return P1;
      }else{
	return A;
      }
    }else{
      return R;
    }
    break;
 case P1:
   if (access_type == 0){
     return P1;
   }else{
     if (GET_REG_CMD(*lcd_ctrl_reg) == 44){
       return A;
     }else{
       return R;
     }
   }
   break;

 case A:
	 return A;
  }
  return R;
};

int TDC_Automata_drv_send_data_pre::runStep(bool access_type, VarList *mainVarlist){
    this->access_type = access_type;
  this->refreshVariables(mainVarlist);
  StateID tmp_currentState = this->getNextState(currentState);
  switch(tmp_currentState) {
  case A:
    //printf("Accepted!");
    this->currentState = tmp_currentState;
    return 1;
    break;
  case R:
    printf("Rejected!");
    this->currentState = tmp_currentState;
    return -1;
   break;
  default:
    this->currentState = tmp_currentState;
    return 0;
  }
};

void TDC_Automata_drv_send_data_pre::refreshVariables(VarList *mainVarList){
	lcd_ctrl_reg = mainVarList->getVarListByPlatformAddr(mainVarList,LCD_CTRL_REG_ADDR)->getVar(); //50
};

void TDC_Automata_drv_send_data_pre::reset(){
	this->currentState = In;
};

TDC_Automata_drv_send_data_pre::TDC_Automata_drv_send_data_pre(VarList *mainVarList) : TDC_Automata("DRV Send Data - PRE"){
  this->refreshVariables(mainVarList);
};
